package com.itheima.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.mapper.ClazzMapper;
import com.itheima.mapper.EmpMapper;
import com.itheima.pojo.Clazz;
import com.itheima.pojo.Emp;
import com.itheima.pojo.PageResult;
import com.itheima.service.ClazzService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class ClazzServiceImpl extends ServiceImpl<ClazzMapper, Clazz> implements ClazzService {
    @Autowired
    private ClazzMapper clazzMapper;
    @Autowired
    private EmpMapper empMapper;



    @Override
    public PageResult<Clazz> findPage(String name, LocalDate begin, LocalDate end, int page, int pageSize) {
        Page<Clazz> pageInfo = new Page<>(page,pageSize);
       LambdaQueryWrapper<Clazz> queryWrapper = new LambdaQueryWrapper<Clazz>()
               .like(!StringUtils.isEmpty(name),Clazz::getName,name)
               .ge(!StringUtils.isEmpty(begin),Clazz::getBeginDate,begin)
               .le(!StringUtils.isEmpty(end),Clazz::getEndDate,end);

       //分页
       this.page(pageInfo,queryWrapper);
       List<Clazz> records = pageInfo.getRecords();
       records.forEach(clazz -> {
           Integer masterId = clazz.getMasterId();
           Emp emp = empMapper.selectById(masterId);
           if (emp !=null){
               clazz.setMasterName(emp.getName());
           }

           if (clazz.getEndDate() != null) {
               if(clazz.getEndDate().isBefore(LocalDate.now())){
                   clazz.setStatus("已结课");
               }else if(clazz.getBeginDate().isAfter(LocalDate.now())){
                 clazz.setStatus("未开始");
               }else {
                   clazz.setStatus("在读");
               }
           }
       });
        return new PageResult<>(pageInfo.getTotal(),pageInfo.getRecords());
    }



    //根据ID查询
    @Override
    public Clazz getClazzById(Integer id) {
        Clazz clazz = this.getById(id);
        if (clazz != null){
            Integer masterId = clazz.getMasterId();
            Emp emp = empMapper.selectById(masterId);
            if (emp !=null){
                clazz.setMasterName(emp.getName());
            }
            return clazz;
        }
        return clazz;
    }

    //新增班级
    @Override
    @Transactional
    public void saveClazz(Clazz clazz) {
        clazz.setCreateTime(LocalDateTime.now());
        clazz.setUpdateTime(LocalDateTime.now());
        this.save(clazz);
    }

    @Override
    public void updateClazz(Clazz clazz) {
        clazz.setUpdateTime(LocalDateTime.now());
        this.updateById(clazz);
    }
}
